package io.renren.controller;

import io.renren.annotation.Login;
import io.renren.constant.AppConstant;
import io.renren.entity.DrivingLicense;
import io.renren.entity.UserDrivingLicense;
import io.renren.entity.UserReservation;
import io.renren.entity.Vehicle;
import io.renren.entity.VioViolationDto;
import io.renren.po.IllegalCode;
import io.renren.po.IllegalCrossingInfo;
import io.renren.po.IllegalVehicleResp;
import io.renren.po.RoadAccidentInfo;
import io.renren.po.UserVehiclePO;
import io.renren.po.UserVehiclePODto;
import io.renren.service.PoliceService;
import io.renren.vo.IllegalCrossingInfoHistryList;
import io.renren.vo.IllegalCrossingInfoList;
import io.renren.vo.IllegalHistoryRedLightRes;
import io.renren.vo.IllegalHistoryRoadRes;
import io.renren.vo.IllegalVehicleHistryRes;
import io.renren.vo.IllegalVehicleList;
import io.renren.vo.IllegalVehicleReq;
import io.renren.vo.Pager;
import io.renren.vo.RespModel;
import io.renren.vo.RoadAccidentInfoList;
import io.renren.vo.ShootReq;
import io.renren.vo.ShootRes;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/police/online")
@Api(tags = "警察在线")
public class PoliceResourceController {

    private static final Logger logger = LoggerFactory.getLogger(PoliceResourceController.class);
    @Autowired
    private PoliceService policeService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        // 首先建立一个可以将字符串转换为日期的工具程序类
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 明确的描述此时需要注册一个日期格式的转化处理程序类
        binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(sdf, false));
    }

    @GetMapping(value = "/query/illegalCode", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询违法代码", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK", response = RespModel.class),
            @ApiResponse(code = 500, message = "服务端异常", response = RespModel.class)}
    )
    @ApiImplicitParam(value = "违法代码", required = true, name = "illegalCode")
//    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel<List<IllegalCode>> queryIllegalCode(@RequestParam("illegalCode") String illegalCode) {
        logger.info("查询违法代码............");
        List<IllegalCode> list = policeService.queryillegalInfo(illegalCode);
        RespModel<List<IllegalCode>> resp = new RespModel<>(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", list);
        return resp;
    }

    @GetMapping("/list/image/{imageType}")
    @ApiOperation(value = "通过业务类型查询图片", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK", response = RespModel.class),
            @ApiResponse(code = 500, message = "服务端异常", response = RespModel.class)})
    @ApiImplicitParams({
            @ApiImplicitParam(value = "业务类型", name = "imageType", required = true),
            @ApiImplicitParam(value = "页号", name = "page", required = true),
            @ApiImplicitParam(value = "分页大小", name = "limit")
    }
    )
//    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel<Pager<ShootRes>> queryImageList(
            @PathVariable("imageType") String imageType,
            Integer page,
            Integer limit) {
        logger.info("=========page:" + page + ";limit:" + limit);
        Pager<ShootRes> pager = policeService.queryShootList(imageType, page, limit);
        RespModel<Pager<ShootRes>> resp = new RespModel<>(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", pager);
        return resp;
    }

    @Login
    @PostMapping("/add/image/{applicationId}")
    @ApiOperation(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK", response = RespModel.class),
            @ApiResponse(code = 500, message = "服务端异常", response = RespModel.class)})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authtoken", value = "登陆令牌", required = true)
    })
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel addImage(
            @RequestBody ShootReq shootReq,
            HttpServletRequest request) {
        String username = (String) request.getAttribute("username");
        shootReq.setUserName(username);
        int flag = policeService.createShoot(shootReq);

        RespModel resp;
        if (flag == 0) {
            resp = new RespModel(AppConstant.STATU_FAIL.getName(), AppConstant.STATU_FAIL.getValue(), "未查出对方手机号", null);
            return resp;
        }
        resp = new RespModel(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "处理中请稍后....", null);
        return resp;
    }


    /**
     * 查询违法车辆排行榜
     *
     * @param illegalType
     * @return
     */
    @GetMapping("/queryIllegalVehicle/{illegalType}")
    @ApiOperation(value = "查询违法车辆排行榜", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK", response = RespModel.class),
            @ApiResponse(code = 500, message = "服务端异常", response = RespModel.class)})
    @ApiImplicitParam(name = "illegalType", required = true, value = "违法类型")
//    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel<IllegalVehicleList> queryIllegalVehicle(@PathVariable("illegalType") String illegalType) {
        if (illegalType == null) {
            return new RespModel<>(AppConstant.STATU_FAIL.getName(), AppConstant.PARAM_IS_MISS.getValue(), AppConstant.PARAM_IS_MISS.getName(), null);
        }

        try {
            IllegalVehicleList ill = policeService.queryIllegalVehicle(illegalType);
            return new RespModel<>(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", ill);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return new RespModel<>(AppConstant.STATU_FAIL.getName(), AppConstant.SYSTEM_IS_EXCEPTION.getValue(), AppConstant.SYSTEM_IS_EXCEPTION.getName(), null);
        }
    }

    /**
     * 查询违法车辆历史时间集合
     *
     * @param illegalVehicleReq
     * @return
     */
    @PostMapping(value = "/queryIllegalVehicleHistry", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = " 查询违法车辆历史时间集合", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK", response = RespModel.class),
            @ApiResponse(code = 500, message = "服务端异常", response = RespModel.class)})
    @ApiImplicitParam(value = "违法类型和时间请求对象", name = "illegalVehicleReq", dataType = "IllegalVehicleReq")
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel<IllegalVehicleHistryRes> queryIllegalVehicleHistry(@RequestBody IllegalVehicleReq illegalVehicleReq) {
        try {
            IllegalVehicleHistryRes ivr = policeService.queryIllegalVehicleHistry(illegalVehicleReq);
            return new RespModel<>(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", ivr);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return new RespModel<>(AppConstant.STATU_FAIL.getName(), AppConstant.SYSTEM_IS_EXCEPTION.getValue(), AppConstant.SYSTEM_IS_EXCEPTION.getName(), null);
        }
    }

    /**
     * 根据时间查询具体某一时间车辆违法排行榜
     *
     * @param illegalVehicleReq
     * @return
     */
    @PostMapping(value = "/queryIllegalVehicleHistryInfo", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "根据时间查询具体某一时间车辆违法排行榜", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK", response = RespModel.class),
            @ApiResponse(code = 500, message = "服务端异常", response = RespModel.class)})
    @ApiImplicitParam(value = "违法类型和时间请求对象", name = "illegalVehicleReq")
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel<IllegalVehicleList> queryIllegalVehicleHistryInfo(IllegalVehicleReq illegalVehicleReq) {
        if (illegalVehicleReq == null) {
            return new RespModel<>(AppConstant.STATU_FAIL.getName(), AppConstant.PARAM_IS_MISS.getValue(), AppConstant.PARAM_IS_MISS.getName(), null);
        }
        try {
            IllegalVehicleList ill = policeService.queryIllegalVehicleHistryInfo(illegalVehicleReq);
            return new RespModel<>(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", ill);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return new RespModel<>(AppConstant.STATU_FAIL.getName(), AppConstant.SYSTEM_IS_EXCEPTION.getValue(), AppConstant.SYSTEM_IS_EXCEPTION.getName(), null);
        }
    }

    /**
     * 查询违法车辆排行榜内容明细
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryIllegalVehicleInfo/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询违法车辆排行榜内容明细", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK", response = RespModel.class),
            @ApiResponse(code = 500, message = "服务端异常", response = RespModel.class)})
    @ApiImplicitParam(value = "记录ID", name = "id", dataType = "Integer")
//    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel<IllegalVehicleResp> queryIllegalVehicleInfo(@PathVariable int id) {
        if (id == 0) {
            return new RespModel<>(AppConstant.STATU_FAIL.getName(), AppConstant.PARAM_IS_MISS.getValue(), AppConstant.PARAM_IS_MISS.getName(), null);
        }
        try {
            IllegalVehicleResp illVeh = policeService.queryIllegalVehicleInfo(id);
            return new RespModel<>(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", illVeh);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return new RespModel<>(AppConstant.STATU_FAIL.getName(), AppConstant.SYSTEM_IS_EXCEPTION.getValue(), AppConstant.SYSTEM_IS_EXCEPTION.getName(), null);
        }
    }

    @GetMapping(value = "/queryIllegalCrossing", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询违法高发路口最新排行榜", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK", response = RespModel.class),
            @ApiResponse(code = 500, message = "服务端异常", response = RespModel.class)})
//    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel<IllegalCrossingInfoList> queryIllegalCrossing() {
        try {
            IllegalCrossingInfoList ili = policeService.queryIllegalCrossing();
            return new RespModel<>(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", ili);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return new RespModel<>(AppConstant.STATU_FAIL.getName(), AppConstant.SYSTEM_IS_EXCEPTION.getValue(), AppConstant.SYSTEM_IS_EXCEPTION.getName(), null);
        }
    }

    @GetMapping(value = "/queryIllegalHistry/{collectionTime}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询违法高发路口历史时间集合", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK", response = RespModel.class),
            @ApiResponse(code = 500, message = "服务端异常", response = RespModel.class)})
    @ApiImplicitParam(value = "查询时间点", name = "collectionTime", dataTypeClass = Long.class)
//    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel<IllegalHistoryRedLightRes> queryIllegalHistry(@PathVariable("collectionTime") long collectionTime) {

        try {
            Long time = Long.valueOf(collectionTime);
            if (time == null) {
                return new RespModel<>(AppConstant.STATU_FAIL.getName(), AppConstant.PARAM_IS_MISS.getValue(), AppConstant.PARAM_IS_MISS.getName(), null);
            }
            IllegalHistoryRedLightRes illegalHistoryRedLightRes = policeService.queryIllegalHistry(collectionTime);
            return new RespModel<>(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", illegalHistoryRedLightRes);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return new RespModel<>(AppConstant.STATU_FAIL.getName(), AppConstant.SYSTEM_IS_EXCEPTION.getValue(), AppConstant.SYSTEM_IS_EXCEPTION.getName(), null);
        }
    }


    @GetMapping(value = "/queryIllegalHistryInfo/{collectionTime}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询违法闯红灯高发路口历史记录详细", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK", response = RespModel.class),
            @ApiResponse(code = 500, message = "服务端异常", response = RespModel.class)})
//    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel<IllegalCrossingInfoHistryList> queryIllegalHistryInfo(@PathVariable long collectionTime) {
        try {
            Long time = Long.valueOf(collectionTime);
            if (time == null) {
                return new RespModel<>(AppConstant.STATU_FAIL.getName(), AppConstant.PARAM_IS_MISS.getValue(), AppConstant.PARAM_IS_MISS.getName(), null);
            }
            IllegalCrossingInfoHistryList ili = policeService.queryIllegalHistryInfo(collectionTime);
            return new RespModel<>(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", ili);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return new RespModel<>(AppConstant.STATU_FAIL.getName(), AppConstant.SYSTEM_IS_EXCEPTION.getValue(), AppConstant.SYSTEM_IS_EXCEPTION.getName(), null);
        }
    }

    @GetMapping(value = "/queryIllegalInfo/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询违法高发路口详细", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK", response = RespModel.class),
            @ApiResponse(code = 500, message = "服务端异常", response = RespModel.class)})
    @ApiImplicitParam(value = "记录ID", dataType = "int", name = "id")
//    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel<IllegalCrossingInfo> queryIllegalInfo(@PathVariable("id") int id) {
        try {
            if (id == 0) {
                return new RespModel<>(AppConstant.STATU_FAIL.getName(), AppConstant.PARAM_IS_MISS.getValue(), AppConstant.PARAM_IS_MISS.getName(), null);
            }
            IllegalCrossingInfo illegalCrossingInfos = policeService.queryIllegalInfo(id);
            return new RespModel<>(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", illegalCrossingInfos);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return new RespModel<>(AppConstant.STATU_FAIL.getName(), AppConstant.SYSTEM_IS_EXCEPTION.getValue(), AppConstant.SYSTEM_IS_EXCEPTION.getName(), null);
        }
    }

    @GetMapping(value = "/queryRoadAccidents", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询事故高发路口排行榜", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK", response = RespModel.class),
            @ApiResponse(code = 500, message = "服务端异常", response = RespModel.class)})
//    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel<RoadAccidentInfoList> queryRoadAccidents() {
        try {
            RoadAccidentInfoList rai = policeService.queryRoadAccidents();
            return new RespModel<>(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", rai);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return new RespModel<>(AppConstant.STATU_FAIL.getName(), AppConstant.SYSTEM_IS_EXCEPTION.getValue(), AppConstant.SYSTEM_IS_EXCEPTION.getName(), null);
        }
    }


    /**
     * 查询事故高发路口历史列表
     *
     * @param collectionTime
     * @return
     */
    @GetMapping(value = "/queryAccidentsHistry/{collectionTime}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询事故高发路口历史时间列表", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK", response = RespModel.class),
            @ApiResponse(code = 500, message = "服务端异常", response = RespModel.class)})
    @ApiImplicitParam(name = "collectionTime", value = "最新记录时间", required = true, dataType = "Long")
//    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel<IllegalHistoryRoadRes> queryAccidentsHistry(@PathVariable("collectionTime") long collectionTime) {
        try {
            Long time = Long.valueOf(collectionTime);
            if (time == null) {
                return new RespModel<>(AppConstant.STATU_FAIL.getName(), AppConstant.PARAM_IS_MISS.getValue(), AppConstant.PARAM_IS_MISS.getName(), null);
            }
            IllegalHistoryRoadRes illegalHistoryRoadRes = policeService.queryAccidentsHistry(collectionTime);
            return new RespModel<>(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", illegalHistoryRoadRes);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return new RespModel<>(AppConstant.STATU_FAIL.getName(), AppConstant.SYSTEM_IS_EXCEPTION.getValue(), AppConstant.SYSTEM_IS_EXCEPTION.getName(), null);
        }
    }

    /**
     * 查询事故高发路口历史详细
     *
     * @param collectionTime
     * @return
     */
    @GetMapping(value = "/queryAccidentsHistryInfo/{collectionTime}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询事故高发路口历史详细", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK", response = RespModel.class),
            @ApiResponse(code = 500, message = "服务端异常", response = RespModel.class)})
    @ApiImplicitParam(name = "collectionTime", value = "历史时间", required = true, dataType = "Long")
//    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel<RoadAccidentInfoList> queryAccidentsHistryInfo(@PathVariable long collectionTime) {
        try {
            Long time = Long.valueOf(collectionTime);
            if (time == null) {
                return new RespModel<>(AppConstant.STATU_FAIL.getName(), AppConstant.PARAM_IS_MISS.getValue(), AppConstant.PARAM_IS_MISS.getName(), null);
            }
            RoadAccidentInfoList rai = policeService.queryAccidentsHistryInfo(collectionTime);
            return new RespModel<>(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", rai);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return new RespModel<>(AppConstant.STATU_FAIL.getName(), AppConstant.SYSTEM_IS_EXCEPTION.getValue(), AppConstant.SYSTEM_IS_EXCEPTION.getName(), null);
        }
    }

    /**
     * 查询事故高发路口详细
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryRoadAccidentsInfo/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询事故高发路口详细记录", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK", response = RespModel.class),
            @ApiResponse(code = 500, message = "服务端异常", response = RespModel.class)})
    @ApiImplicitParam(name = "id", value = "事故ID", required = true, dataType = "int")
//    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel<RoadAccidentInfo> queryRoadAccidentsInfo(@PathVariable("id") int id) {
        try {
            if (id == 0) {
                return new RespModel<>(AppConstant.STATU_FAIL.getName(), AppConstant.PARAM_IS_MISS.getValue(), AppConstant.PARAM_IS_MISS.getName(), null);
            }
            RoadAccidentInfo roadAccidentInfos = policeService.queryRoadAccidentsInfo(id);
            return new RespModel<>(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", roadAccidentInfos);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return new RespModel<>(AppConstant.STATU_FAIL.getName(), AppConstant.SYSTEM_IS_EXCEPTION.getValue(), AppConstant.SYSTEM_IS_EXCEPTION.getName(), null);
        }
    }

    @GetMapping(value = "/getReservationVehicleList/{userid}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取车辆列表接口", produces = MediaType.APPLICATION_JSON_VALUE)
//    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel<List<UserVehiclePO>> getReservationVehicleList(@PathVariable String userid) {
        List<UserVehiclePO> userVehiclePOS = policeService.getVehicleList(userid);
        return new RespModel(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", userVehiclePOS);
    }

    @PostMapping(value = "/saveReservationVehicle", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "保存用户车辆信息", produces = MediaType.APPLICATION_JSON_VALUE)
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel saveReservationVehicle(@RequestBody @Valid UserVehiclePO userVehiclePO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            StringBuilder stringBuilder = new StringBuilder();
            bindingResult.getFieldErrors().stream().forEach(fieldError ->
                    stringBuilder.append(fieldError.getDefaultMessage()).append(";")
            );
            return new RespModel(AppConstant.PARAM_IS_MISS.getName(), AppConstant.PARAM_IS_MISS.getValue(), stringBuilder.toString(), null);
        } else {
            return policeService.saveReservationVehicle(userVehiclePO);
        }
    }

    @PostMapping(value = "/getReservationBasicInfo", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询用户信誉积分", produces = MediaType.APPLICATION_JSON_VALUE)
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel getReservationBasicInfo(@Valid UserVehiclePO userVehiclePO, BindingResult bindResult) {
        if (bindResult.hasErrors()) {
            if (!(bindResult.getAllErrors().size() == 1
                    && bindResult.getAllErrors().get(0).getDefaultMessage().equals("发动机号不能为空"))) {
                StringBuilder sb = new StringBuilder();
                bindResult.getAllErrors().forEach(filederror
                        -> sb.append(filederror.getDefaultMessage()).append(";"));
                return new RespModel(AppConstant.PARAM_IS_MISS.getName(), AppConstant.PARAM_IS_MISS.getValue(), sb.toString(), null);
            }
        }
        return policeService.getReservationBasicInfo(userVehiclePO);
    }

    @PostMapping(value = "/getCheckSiteList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取审车点列表", produces = MediaType.APPLICATION_JSON_VALUE)
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel getCheckSiteList() {
        List<Map> checkSiteList = policeService.getCheckSiteList();
        return new RespModel(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", checkSiteList);
    }

    @PostMapping(value = "/getCheckSiteServiceProject/{company_id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询检测站服务项目", produces = MediaType.APPLICATION_JSON_VALUE)
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel getCheckSiteServiceProject(@PathVariable String company_id) {
        List<Map> serviceProject = policeService.queryCheckSiteServiceProject(company_id);
        return new RespModel(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", serviceProject);
    }

    @PostMapping(value = "/getReservationTimeList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取可预约时间列表", produces = MediaType.APPLICATION_JSON_VALUE)
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel getReservationTimeList(@RequestBody @Valid UserReservation userReservation, BindingResult bindingResult) {
        if (bindingResult.getFieldError("company_id") != null
                || userReservation.getReservation_date() == null) {
            StringBuilder sb = new StringBuilder();
            sb.append(bindingResult.getFieldError("company_id")).append(";");
            if (userReservation.getReservation_date() == null) sb.append("预约日期不能为空").append(";");
            return new RespModel(AppConstant.PARAM_IS_MISS.getName(), AppConstant.PARAM_IS_MISS.getValue(), sb.toString(), null);
        }

        if (userReservation.getReservation_date().before(new Date())) {
            return new RespModel(AppConstant.STATU_FAIL.getName(), AppConstant.STATU_FAIL.getValue(), "只能预约当前日期或之后的日期", null);
        }
        return policeService.getReservationTimeList(userReservation);
    }

    @PostMapping(value = "/submitReservationTime", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "提交预约审车", produces = MediaType.APPLICATION_JSON_VALUE)
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel submitReservationTime(@RequestBody @Valid UserReservation userReservation, BindingResult bindingResult) {
        if (bindingResult.hasErrors() || userReservation.getReservation_time() == null) {
            StringBuilder sb = new StringBuilder();
            if (userReservation.getReservation_time() == null) sb.append("预约时间不能为空").append(";");
            bindingResult.getFieldErrors().forEach(filederror -> sb.append(filederror.getDefaultMessage()).append(";"));
            return new RespModel(AppConstant.PARAM_IS_MISS.getValue(),
                    AppConstant.PARAM_IS_MISS.getValue(),
                    sb.toString(),
                    null);
        }
        return policeService.submitReservationTime(userReservation);
    }

    @Login
    @PostMapping(value = "/getReservationRecord/{applicationId}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询年审预约记录", produces = MediaType.APPLICATION_JSON_VALUE)
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel getReservationRecord(String license_plate, HttpServletRequest request) {
//        if (StringUtils.isEmpty(license_plate)) {
//            return new RespModel(AppConstant.PARAM_IS_MISS.getName(), AppConstant.PARAM_IS_MISS.getValue(), "", null);
//        }
        String userId = ((String) request.getAttribute("username"));
        List<Map> maps = policeService.queryReservationRecord(license_plate, userId);
        return new RespModel(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(),
                "", maps);
    }

    @Login
    @PostMapping(value = "/cancelReservationTime/{applicationId}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "取消预约", produces = MediaType.APPLICATION_JSON_VALUE)
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel cancelReservationTime(@Valid UserReservation userReservation,
                                           BindingResult bindingResult,
                                           HttpServletRequest request) {
        String errorMessage = getErrorMessage(bindingResult, "reservation_id");
        if (!errorMessage.isEmpty()) {
            return new RespModel<>(AppConstant.PARAM_IS_MISS.getName(), AppConstant.PARAM_IS_MISS.getValue()
                    , errorMessage, null);
        }
        userReservation.setUserid(((String) request.getAttribute("username")));
        return policeService.cancelReservationTime(userReservation);
    }

    @Login
    @PostMapping(value = "/submitReservationArrive/{applicationId}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "到达预约检测站点", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "license_plate", required = true),
            @ApiImplicitParam(name = "userid", required = true),
            @ApiImplicitParam(name = "reservation_id", required = true)
    })
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel submitReservationArrive(@Valid UserReservation userReservation, BindingResult bindingResult, HttpServletRequest request) {
        String errorMessage = getErrorMessage(bindingResult, "reservation_id");
        if (!errorMessage.isEmpty()) {
            return new RespModel<>(AppConstant.PARAM_IS_MISS.getName(), AppConstant.PARAM_IS_MISS.getValue()
                    , errorMessage, null);
        }
        String userid = ((String) request.getAttribute("username"));
        userReservation.setArrive_state(1);
        userReservation.setUserid(userid);
        Integer update_count = policeService.updateReservationArrive(userReservation);
        if (update_count == null || update_count == 0) {
            return new RespModel(AppConstant.STATU_FAIL.getName(), AppConstant.STATU_FAIL.getValue(),
                    "更新失败，没有预约记录", null);
        }
        return new RespModel<>(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", null);
    }

    @Login
    @PostMapping(value = "/deleteUserVehicle/{applicationId}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除用户绑定的车辆")
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel deleteUserVehicle(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        String license_plate = String.valueOf(params.get("license_plate"));
        if (StringUtils.isEmpty(license_plate)) {
            return new RespModel().getRespModelInstance(AppConstant.PARAM_IS_MISS, "缺少车牌号", null);
        }
        Integer integer = policeService.deleteUserVehicle(((String) request.getAttribute("username")), license_plate);
        if (integer == null || integer != 1) {
            return new RespModel().getRespModelInstance(AppConstant.STATU_FAIL, "没有删除任何记录", null);
        } else {
            return new RespModel().getRespModelInstance(AppConstant.STATU_SUCCESS, "", null);
        }
    }

    /**
     * 过滤校验，关注需要校验的字段信息
     *
     * @param bindingResult
     * @param args
     * @return
     */
    private String getErrorMessage(BindingResult bindingResult, String... args) {
        StringBuilder sb = new StringBuilder();
        Arrays.stream(args).forEach(filedName -> {
            FieldError fieldError = bindingResult.getFieldError(filedName);
            if (fieldError != null && !StringUtils.isEmpty(fieldError.getDefaultMessage())) {
                sb.append(fieldError.getDefaultMessage()).append(";");
            }
        });
        return sb.toString();
    }

    /**
     * APP端获取车辆违法信息列表
     *
     * @param vehicle
     * @return
     */
    @PostMapping(value = "/getVioViolationList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取车辆违法信息列表接口", produces = MediaType.APPLICATION_JSON_VALUE)
//    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel getVioViolationList(Vehicle vehicle) {
        if (vehicle == null || StringUtils.isBlank(vehicle.getHPZL()) || StringUtils.isBlank(vehicle.getHPHM()) || StringUtils.isBlank(vehicle.getFDJH())) {
            return new RespModel<>().getRespModelInstance(AppConstant.PARAM_IS_MISS, null, null);
        }
        return policeService.getVioViolationList(vehicle);
    }


    /**
     * 获取其他人驾照信息
     *
     * @param drivingLicense
     * @return
     */
    @PostMapping(value = "/getOthersDrivingLicense", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取其他人驾驶证信息接口", produces = MediaType.APPLICATION_JSON_VALUE)
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel<UserDrivingLicense> getOthersDrivingLicense(DrivingLicense drivingLicense) {
        if (drivingLicense == null ||
                StringUtils.isBlank(drivingLicense.getSFZMHM()) || StringUtils.isBlank(drivingLicense.getDABH())) {
            return new RespModel<>().getRespModelInstance(AppConstant.PARAM_IS_MISS, null, null);
        }
        return policeService.getOthersDrivingLicense(drivingLicense);
    }


    /**
     * 获取其他人车辆信息
     *
     * @param vehicle
     * @return
     */
    @PostMapping(value = "/getOthersVehicle", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取其他人车辆信息接口", produces = MediaType.APPLICATION_JSON_VALUE)
//    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel<Vehicle> getOthersVehicle(Vehicle vehicle) {
        if (vehicle == null || StringUtils.isBlank(vehicle.getHPZL()) || StringUtils.isBlank(vehicle.getHPHM()) || StringUtils.isBlank(vehicle.getFDJH())) {
            return new RespModel<>().getRespModelInstance(AppConstant.PARAM_IS_MISS, null, null);
        }
        return policeService.getOthersVehicle(vehicle);
    }

    /**
     * APP端获取车辆列表
     *
     * @param userid
     * @return
     */
    @GetMapping(value = "/getReservationVehicleList2/{userid}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取车辆列表接口", produces = MediaType.APPLICATION_JSON_VALUE)
//    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel<List<UserVehiclePO>> getReservationVehicleList2(@PathVariable String userid) {
        List<UserVehiclePODto> userVehiclePOS = policeService.getAppVehicleList(userid);
        return new RespModel(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", userVehiclePOS);
    }


    /**
     * APP端获取车辆违法信息列表(用于违法数量角标)
     *
     * @param userid 用户id
     * @return
     */
    @GetMapping(value = "/getVioViolationList2/{userid}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取车辆列表接口", produces = MediaType.APPLICATION_JSON_VALUE)
//    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel getVioViolationList2(@PathVariable String userid) {
        List<UserVehiclePODto> userVehiclePOS = policeService.getAppVehicleList(userid);

        List<VioViolationDto> resultList = new ArrayList<>();
        for (UserVehiclePODto po : userVehiclePOS) {
            Vehicle vehicle = new Vehicle();
            vehicle.setHPZL(po.getCarType());
            vehicle.setHPHM(po.getCarNo());
            vehicle.setFDJH(po.getFdjh());
            RespModel respModel = policeService.getVioViolationList(vehicle);
            Object o = respModel.getResult();
            if (o != null) {
                resultList.addAll((List<VioViolationDto>) o);
            }
        }
        return new RespModel(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", resultList);
    }


    /**
     * 查询驾照信息
     *
     * @param userid
     * @return
     */
    @GetMapping(value = "/getUserDrivingLicense/{userid}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取驾驶证信息接口", produces = MediaType.APPLICATION_JSON_VALUE)
//    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel<UserDrivingLicense> getUserDrivingLicense(@PathVariable String userid) {
        if ("undefined".equals(userid)) {
            return new RespModel<>().getRespModelInstance(AppConstant.TOKEN_EXCEPTION, null, null);
        }
        return policeService.getDrivingLicense(userid);
    }

    /**
     * 保存驾照信息
     *
     * @param userDrivingLicense 驾照信息
     * @param bindingResult
     * @return
     */
    @PostMapping(value = "/saveUserDrivingLicense", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "保存用户驾照信息", produces = MediaType.APPLICATION_JSON_VALUE)
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel saveUserDrivingLicense(@RequestBody @Valid UserDrivingLicense userDrivingLicense, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            StringBuilder stringBuilder = new StringBuilder();
            bindingResult.getFieldErrors().stream().forEach(fieldError ->
                    stringBuilder.append(fieldError.getDefaultMessage()).append(";")
            );
            return new RespModel(AppConstant.PARAM_IS_MISS.getName(), AppConstant.PARAM_IS_MISS.getValue(), stringBuilder.toString(), null);
        } else {
            return policeService.saveUserDrivingLicense(userDrivingLicense);
        }
    }


    /**
     * 删除驾照信息
     *
     * @param request
     * @return
     */
    @Login
    @PostMapping("/deleteUserDrivingLicense/{applicationId}")
    @ApiOperation(value = "删除用户绑定的驾驶证信息")
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel deleteUserDrivingLicense(HttpServletRequest request) {
        String userid = String.valueOf(request.getAttribute("username"));
        if (StringUtils.isEmpty(userid)) {
            return new RespModel().getRespModelInstance(AppConstant.PARAM_IS_MISS, "驾驶证信息不存在", null);
        }
        Integer integer = policeService.deleteUserDrivingLicense(userid);
        if (integer == null || integer != 1) {
            return new RespModel().getRespModelInstance(AppConstant.STATU_FAIL, "没有删除任何记录", null);
        } else {
            return new RespModel().getRespModelInstance(AppConstant.STATU_SUCCESS, "", null);
        }
    }


}
